﻿Public Class AIZeroReviseForm

    Private _pi As ProjectItem

    '选择设备的网络地址
    Private _netID As Byte = 0

    '起始地址
    Private _offset As UShort = 2110

    Private _offsetEx16 As UShort = 2030   '兼容模块零点修正起始地址

    Private _offsetType As UShort = 12

    Private buf() As UShort
    Private bufEc(32) As UShort

    Dim buf1() As UShort

    Private ExTypeItem As Boolean  '兼容模块类型中是否存在TBCC类型

    Private Ex16Type As Integer '选择设备类型

    Private _offsetNum As UShort = 2010   '兼容模块数量起始地址
    Private ExAiNum As Integer   '兼容模块配置AI数量

    '正在修改参数标志
    Private _isModify As Boolean = False


    Private Sub ClearData()
        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                CType(ctl, NumericUpDown).Value = 0
            End If
        Next
    End Sub

    Private Sub LoadAllRef()
        Dim tn As TreeNode


        Dim dts As New DevTypeService(_pi)

        For Each dti As DevTypeItem In dts.GeDevGroupList()   '设备列表
            tn = Me.TreeView1.Nodes.Add(String.Format("{0}号设备", dti.NetId))
            tn.Tag = dti
        Next

        Me.TreeView1.ExpandAll()

    End Sub


    Private Sub TBCCAIReviseForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _pi = MainForm.pi

        ClearData()

        Me.HideAllControl()


        LoadAllRef()

    End Sub

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim result As Boolean = False
        Dim resultRe As Boolean = False
        Me.Cursor = Cursors.WaitCursor
        ' Dim devtype As Integer
        'devtype = DevTypeService.GetdevType(MainForm.pi.ProjectID, _netID)

        TreeView1.Enabled = False

        Me.btnOK.Enabled = False
        Me.btnCancel.Enabled = False
        Me.RevisePanel.Enabled = False
        Me._isSaving = True
        'Select Case Ex16Type
        '    Case 1, 17
        '        result = Save()
        '    Case 16, 18
        result = SaveEx16()

        'End Select

        Me._isSaving = False
        Me.btnOK.Enabled = True
        Me.btnCancel.Enabled = True
        Me.RevisePanel.Enabled = True

        Me.Cursor = Cursors.Default

        If result Then
            EnableModify(False)
            msg.Show("参数保存成功")
            'Select Case Ex16Type
            '    Case 1, 17
            '        RefreshData() '重新读取数据
            '    Case 16, 18
            '        Me.btnModify.Enabled = False
            RefreshDataEx16() '重新读取数据
            'resultRe = RefreshDataEx16() '重新读取数据
            'If resultRe Then
            '    Me.btnModify.Enabled = True
            'Else

            '    Me.btnModify.Enabled = False

            'End If

            'End Select




        Else
            msg.ShowError("参数保存失败")
        End If

        TreeView1.Enabled = True

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Me.btnModify.Enabled = False

        'Select Case Ex16Type
        '    Case 1, 17

        If EnterModify() = False Then
            msg.Show("无法修改参数请重试")
            Me.btnModify.Enabled = True
            Exit Sub
        End If
        EnableModify(True)
        '    Case 16, 18

        '        '开始发送心跳包
        '        Me.TimerHeart.Start()
        '        EnableModifyEc(True)



        'End Select
       


    End Sub

    Private Sub EnableModify(ByVal value As Boolean)

        Me.btnModify.Enabled = Not value
        Me.btnOK.Enabled = value

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)

                nud.Increment = IIf(value, 1, 0)
                'If nud.Tag > ExAiNum - 1 Then
                '    nud.ReadOnly = False
                'Else

                nud.ReadOnly = Not value

                'End If
            End If
        Next

    End Sub
    Private Sub EnableModifyEc(ByVal value As Boolean)

        Me.btnModify.Enabled = Not value
        Me.btnOK.Enabled = value

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)

                nud.Increment = IIf(value, 1, 0)
                'If nud.Tag > ExAiNum - 1 Then
                '    nud.Enabled = False

                'Else

                nud.ReadOnly = Not value

                'End If
            End If
        Next

    End Sub

    '隐藏/显示Panel中的所有控件
    Private Sub HideAllControl()
        For Each ctl As Control In Me.RevisePanel.Controls
            ctl.Visible = False
            If ctl.GetType Is GetType(NumericUpDown) Then
                CType(ctl, NumericUpDown).ReadOnly = True
                CType(ctl, NumericUpDown).Increment = False
            End If
        Next

        Me.btnModify.Enabled = False
        Me.btnOK.Enabled = False
        Me.lbl_tip.Visible = True
        Me.lblSelected.Text = ""
    End Sub

    '根据设备类型修改panel中numupdw控件的布局
    Private Sub SetPanelLayout(ByVal DevType As Integer, ByVal enable As Boolean)

        NumericUpDown1.Tag = 0
        NumericUpDown2.Tag = 1
        NumericUpDown3.Tag = 2
        NumericUpDown4.Tag = 3
        NumericUpDown5.Tag = 4
        NumericUpDown6.Tag = 5

        NumericUpDown7.Tag = 6
        NumericUpDown8.Tag = 7
        NumericUpDown9.Tag = 8
        NumericUpDown10.Tag = 9
        NumericUpDown11.Tag = 10
        NumericUpDown12.Tag = 11
        Label7.Text = "7# AI端口修正系数:"
        Label8.Text = "8# AI端口修正系数:"

        NumericUpDown1.Visible = True
        NumericUpDown2.Visible = True
        NumericUpDown3.Visible = True
        NumericUpDown4.Visible = True
        NumericUpDown5.Visible = True
        NumericUpDown6.Visible = True
        NumericUpDown7.Visible = True
        NumericUpDown8.Visible = True

        NumericUpDown9.Visible = True
        NumericUpDown10.Visible = True
        NumericUpDown11.Visible = True
        NumericUpDown12.Visible = True

        NumericUpDown13.Visible = True
        NumericUpDown14.Visible = True
        NumericUpDown15.Visible = True
        NumericUpDown16.Visible = True

        Label1.Visible = NumericUpDown1.Visible
        Label2.Visible = NumericUpDown2.Visible
        Label3.Visible = NumericUpDown3.Visible
        Label4.Visible = NumericUpDown4.Visible
        Label5.Visible = NumericUpDown5.Visible
        Label6.Visible = NumericUpDown6.Visible
        Label7.Visible = NumericUpDown7.Visible
        Label8.Visible = NumericUpDown8.Visible
        Label9.Visible = NumericUpDown9.Visible
        Label10.Visible = NumericUpDown10.Visible
        Label11.Visible = NumericUpDown11.Visible
        Label12.Visible = NumericUpDown12.Visible

        Label13.Visible = NumericUpDown13.Visible
        Label14.Visible = NumericUpDown14.Visible
        Label15.Visible = NumericUpDown15.Visible
        Label16.Visible = NumericUpDown16.Visible
       

    End Sub
    Private Sub SetPanelLayoutEx16(ByVal DevType As Integer, ByVal enable As Boolean)
        NumericUpDown1.Tag = 0
        NumericUpDown2.Tag = 1
        NumericUpDown3.Tag = 2
        NumericUpDown4.Tag = 3
        NumericUpDown5.Tag = 4
        NumericUpDown6.Tag = 5
        NumericUpDown7.Tag = 6
        NumericUpDown8.Tag = 7
        NumericUpDown9.Tag = 8
        NumericUpDown10.Tag = 9
        NumericUpDown11.Tag = 10
        NumericUpDown12.Tag = 11
        NumericUpDown13.Tag = 12
        NumericUpDown14.Tag = 13
        NumericUpDown15.Tag = 14
        NumericUpDown16.Tag = 15

        NumericUpDown1.Visible = True
        NumericUpDown2.Visible = True
        NumericUpDown3.Visible = True
        NumericUpDown4.Visible = True
        NumericUpDown5.Visible = True
        NumericUpDown6.Visible = True
        NumericUpDown7.Visible = True
        NumericUpDown8.Visible = True
        NumericUpDown9.Visible = True
        NumericUpDown10.Visible = True
        NumericUpDown11.Visible = True
        NumericUpDown12.Visible = True
        NumericUpDown13.Visible = True
        NumericUpDown14.Visible = True
        NumericUpDown15.Visible = True
        NumericUpDown16.Visible = True

        Label1.Visible = NumericUpDown1.Visible
        Label2.Visible = NumericUpDown2.Visible
        Label3.Visible = NumericUpDown3.Visible
        Label4.Visible = NumericUpDown4.Visible
        Label5.Visible = NumericUpDown5.Visible
        Label6.Visible = NumericUpDown6.Visible
        Label7.Visible = NumericUpDown7.Visible
        Label8.Visible = NumericUpDown8.Visible
        Label9.Visible = NumericUpDown9.Visible
        Label10.Visible = NumericUpDown10.Visible
        Label11.Visible = NumericUpDown11.Visible
        Label12.Visible = NumericUpDown12.Visible

        Label13.Visible = NumericUpDown13.Visible
        Label14.Visible = NumericUpDown14.Visible
        Label15.Visible = NumericUpDown15.Visible
        Label16.Visible = NumericUpDown16.Visible
    End Sub

    ''' <summary>
    ''' 保存所有参数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean

        Dim tic As New TranstrationItem()
        Dim strSql As String

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)
                If nud.Visible = True Then

                    If buf(nud.Tag) <> nud.Value Then
                        '参数已修改，需要保存
                        If Not Save(nud.Tag, nud.Value) Then
                            Return False
                        End If
                        '保存校准日志
                        strSql = String.Format("insert into tbccCalibLog(ProjectID,NetID,CalibTime,CalibType,CalibPort,UserName,OldValue,NewValue) values('{0}',{1},'{2}',{3},{4},'{5}','{6}','{7}' )" _
                        , _pi.ProjectID, _netID, Now.ToString("yyyy-MM-dd HH:mm:ss"), 1, CInt(nud.Tag) + 1, TBCC.UserManage.User.Instance.uName, buf(CInt(nud.Tag)), nud.Value)
                        tic.Add(strSql, Nothing)

                    End If

                End If
               
            End If
        Next

        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(_netID) = False Then
            Return False
        End If

        ' Me.TimerHeart.Stop()

        '修改门变量
        If ExitModify() = False Then
            Debug.Write("ExitModify错误")
        End If

        '执行校准记录保存
        SqlHelper.ExecuteTranstration(tic)

        Return True
    End Function
    ''' <summary>
    ''' 保存所有参数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SaveEx16() As Boolean

        Dim tic As New TranstrationItem()
        Dim strSql As String

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)
                'If nud.Tag > ExAiNum Then
                '    Continue For

                'End If
                If bufEc(nud.Tag) <> nud.Value Then
                    For i As Integer = 0 To 2
                        '参数已修改，需要保存
                        If Not SaveEx16(nud.Tag, nud.Value) Then
                            ' Return False
                        Else
                            Exit For
                        End If

                    Next

                    '保存校准日志
                    strSql = String.Format("insert into tbccCalibLog(ProjectID,NetID,CalibTime,CalibType,CalibPort,UserName,OldValue,NewValue) values('{0}',{1},'{2}',{3},{4},'{5}','{6}','{7}' )" _
                    , _pi.ProjectID, _netID, Now.ToString("yyyy-MM-dd HH:mm:ss"), 1, CInt(nud.Tag) + 1, TBCC.UserManage.User.Instance.uName, bufEc(CInt(nud.Tag)), nud.Value)
                    tic.Add(strSql, Nothing)

                End If
            End If
        Next

       
        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(_netID) = False Then
            Return False
        End If

        ' Me.TimerHeart.Stop()

        '修改门变量
        If ExitModify() = False Then
            Debug.Write("ExitModify错误")
        End If

        '执行校准记录保存
        Return SqlHelper.ExecuteTranstration(tic)


    End Function

    ''' <summary>
    ''' 保存参数
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function save(ByVal index As UShort, ByVal value As UShort) As Boolean
        Dim offset As UShort = _offset + index + 1000
        Return COMHelper.Instance.Write(_netID, FunctionCode.WordWrite, offset, value)
    End Function
    ''' <summary>
    ''' 保存参数
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function saveEx16(ByVal index As UShort, ByVal value As UShort) As Boolean
        Dim offset As UShort = _offsetEx16 + index + 1000
        Return COMHelper.Instance.Write(_netID, FunctionCode.WordWrite, offset, value)
    End Function

    ''' <summary>
    ''' 进入修改
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function EnterModify() As Boolean
        If g_test Then
            _isModify = True
            Return True
        End If

        _isModify = True

        Dim result As Boolean
        result = ParameterModifyClass.EnterModify(_netID)

        If result = False Then
            _isModify = False
            Return False
        End If

        '开始发送心跳包
        Me.TimerHeart.Start()

        Return True
    End Function


    ''' <summary>
    ''' 退出修改
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ExitModify() As Boolean
        '门变量置为1
        Dim result As Boolean
        result = ParameterModifyClass.ExitModify(_netID)

        '停止发送心跳包
        Me.TimerHeart.Stop()

        If result = True Then  '修改门变量成功
            _isModify = False
        End If
        Return True
    End Function


#Region "发送心跳包"

    '心跳包连续发送错误的次数
    Private _heartFailCount As Integer = 0

    '心跳值
    Private _heartValue As Short = 0

    '心跳值是否初始化
    Private _bheartValue As Boolean = False

    '是否正在发送心跳标志
    Private _heartSending As Boolean = False

    '是否正在保存标志
    Public _isSaving As Boolean = False

    '心跳参数ID为选择ID
    Private netid As Byte

    '心跳包地址
    Private _heartIncreamentAddress As UShort = 7063

    ''' <summary>
    ''' 发送心跳包
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub HeartSend()
        If _bheartValue = False Then
            Dim b() As Short

            netid = _netID
            b = COMHelper.Instance.Send4Short(netid, FunctionCode.WordWritableRead, _heartIncreamentAddress, 1)
            If b IsNot Nothing AndAlso b.Length = 1 Then
                _heartValue = b(0)
                _bheartValue = True
            Else
                Exit Sub
            End If
        End If

        _heartValue = (_heartValue + 1) Mod UShort.MaxValue

        Dim result As Boolean
        result = COMHelper.Instance.Write(netid, FunctionCode.WordWrite, _heartIncreamentAddress, _heartValue)

        If result Then
            _heartFailCount = 0
        Else
            _heartFailCount += 1
        End If

        If _heartFailCount > 10 Then
            '提示用户可能断开连接了.
        End If

    End Sub

    ''' <summary>
    ''' 心跳定时器Tick事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TimerHeart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHeart.Tick
        If Not g_HeartSend Then
            Exit Sub
        End If

        If _heartSending OrElse _isSaving Then
            Exit Sub
        End If

        _heartSending = True
        Me.HeartSend()
        _heartSending = False
    End Sub

#End Region

    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        If Me._isModify Then
            Me.ExitModify()
        End If
        MyBase.OnClosing(e)
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim tn As TreeNode
        Dim dti As DevTypeItem
        Dim result As Boolean

        tn = Me.TreeView1.SelectedNode

        If tn Is Nothing Then
            Exit Sub
        End If

        If TypeOf tn.Tag Is DevTypeItem Then

            TreeView1.Enabled = False

            ClearData() '切换前先清除显示修正数据

            dti = CType(tn.Tag, DevTypeItem)

            '显示选择设备的名称
            Me.lblSelected.Text = tn.Text

            '关闭设备选择提示
            lbl_tip.Visible = False

            '禁止点击"修改"按钮前修改参数
            Me.btnModify.Enabled = False
            Me.btnOK.Enabled = False

            _netID = dti.NetId   '保存当前选择的设备号

            Ex16Type = dti.DevAppType



            SetToolTipEx16(dti.NetId)
            SetPanelLayoutEx16(dti.DevType, False)


            '查询数据库中探头类型，只有PT1000类型探头允许进行原点修正






            '如果正在修改参数，则取消修改，置位门变量，停止发送心跳包
            If Me._isModify Then
                Me.ExitModify()
            End If


            '读取参数
            result = RefreshDataEx16()

            EnableModifyEc(False)

            If result = False Then
                Me.btnModify.Enabled = False
            Else
                Me.btnModify.Enabled = True
            End If


            TreeView1.Enabled = True

        End If

    End Sub
    ''' <summary>
    ''' 返回兼容模块中模拟传感器数量
    ''' </summary>
    ''' <param name="netid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetEcAivalue(ByVal netid As Integer) As Integer
        Dim k() As UShort = Nothing
        Try
            k = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offsetNum, 1)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default

        End Try

        If k Is Nothing Then

            Return 0
        End If


        Return k(0)
    End Function
    ''' <summary>
    ''' 返回兼容模块类型
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ExType() As Boolean

        Dim k As Integer
        Try
            buf1 = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offsetType, 1)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default

        End Try

        If buf1 Is Nothing Then
            Me.btnModify.Enabled = False
            ClearData()
            msg.ShowError("读取参数失败")
            Return False
        End If

        k = buf1(0)


        If k = 1 Then
            Return True
        Else
            Return False
        End If

    End Function
    ''' <summary>
    ''' 刷新AI原点修正数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Function RefreshData() As Boolean

        Me.Cursor = Cursors.WaitCursor

        Try
            buf = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offset, 12)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try

        If buf Is Nothing Then
            Me.btnModify.Enabled = False
            ClearData()
            msg.ShowError("读取参数失败")
            Return False
        End If

        Me.btnModify.Enabled = True

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)
                If nud.Visible = True Then
                    nud.Value = buf(nud.Tag)

                End If

            End If
        Next
        Return True

    End Function
    ''' <summary>
    ''' 刷新AI原点修正数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Function RefreshDataEx16() As Boolean

        Dim bufEx() As UShort = Nothing
        Me.LabelRead.Text = "数据读取中,请稍等......"

        Me.Cursor = Cursors.WaitCursor

        Try
            bufEc = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offsetEx16, 16)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try

        If bufEc Is Nothing Then
            Me.btnModify.Enabled = False
            ClearData()
            msg.ShowError("读取参数失败")
            Return False
        End If

        Me.btnModify.Enabled = True

        For Each ctl As Control In Me.RevisePanel.Controls
            If ctl.GetType Is GetType(NumericUpDown) Then
                Dim nud As NumericUpDown = CType(ctl, NumericUpDown)
                If nud.Visible = True Then
                    nud.Value = bufEc(nud.Tag)

                End If

            End If
        Next


       

        Me.LabelRead.Text = ""
      
        Return True

    End Function

    Private labList(15) As Label
    Private nudList(15) As NumericUpDown
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        labList(0) = Label1
        labList(1) = Label2
        labList(2) = Label3
        labList(3) = Label4
        labList(4) = Label5
        labList(5) = Label6
        labList(6) = Label7
        labList(7) = Label8
        labList(8) = Label9
        labList(9) = Label10
        labList(10) = Label11
        labList(11) = Label12
        labList(12) = Label13
        labList(13) = Label14
        labList(14) = Label15
        labList(15) = Label16
        For Each l As Label In labList
            ToolTip1.SetToolTip(l, "未使用")
        Next

        nudList(0) = NumericUpDown1
        nudList(1) = NumericUpDown2
        nudList(2) = NumericUpDown3
        nudList(3) = NumericUpDown4
        nudList(4) = NumericUpDown5
        nudList(5) = NumericUpDown6
        nudList(6) = NumericUpDown7
        nudList(7) = NumericUpDown8
        nudList(8) = NumericUpDown9
        nudList(9) = NumericUpDown10
        nudList(10) = NumericUpDown11
        nudList(11) = NumericUpDown12
        nudList(12) = NumericUpDown13
        nudList(13) = NumericUpDown14
        nudList(14) = NumericUpDown15
        nudList(15) = NumericUpDown16
        For Each n As NumericUpDown In nudList
            ToolTip1.SetToolTip(n, "未使用")
        Next
    End Sub

    Private Sub SetToolTip(ByVal NetID As Integer)
        Dim portNo As Integer
        Dim str As String
        Dim portType As String = "未知"
        Dim rts As New RefTypeService(_pi)

        For i As Integer = 0 To 11
            ToolTip1.SetToolTip(labList(i), "未使用")
            ToolTip1.SetToolTip(nudList(i), "未使用")
        Next

        For Each rti As RefTypeItem In rts.GeRefGroupList()
            If rti.NetId = NetID Then
                Dim ats As New AiPortTypeService(rti)
                For Each ati As AiPortTypeItem In ats.GetAiGroupList()
                    portNo = ati.PortNo - 1
                    If portNo < 0 Then Continue For
                    If ati.DataType = 1 Then
                        portType = "温度"
                    ElseIf ati.DataType = 2 Then
                        portType = "湿度"
                    End If
                    str = String.Format("{0}: {1}({2})", rti.RefName, ati.PortName, portType)
                    ToolTip1.SetToolTip(labList(portNo), str)
                    ToolTip1.SetToolTip(nudList(portNo), str)
                Next
            End If
        Next

    End Sub
    Private Sub SetToolTipEx16(ByVal NetID As Integer)
        Dim portNo As Integer
        Dim str As String
        Dim portType As String = "未知"
        Dim rts As New RefTypeService(_pi)

        For i As Integer = 0 To 15
            ToolTip1.SetToolTip(labList(i), "未使用")
            ToolTip1.SetToolTip(nudList(i), "未使用")
        Next

        For Each rti As RefTypeItem In rts.GeRefGroupList()
            If rti.NetId = NetID Then
                Dim ats As New AiPortTypeService(rti)
                For Each ati As AiPortTypeItem In ats.GetAiGroupList()
                    portNo = ati.PortNo - 1
                    If portNo < 0 Then Continue For
                    If ati.DataType = 1 Then
                        portType = "温度"
                    ElseIf ati.DataType = 2 Then
                        Continue For
                        portType = "湿度"
                    End If
                    str = String.Format("{0}: {1}({2})", rti.RefName, ati.PortName, portType)
                    ToolTip1.SetToolTip(labList(portNo), str)
                    ToolTip1.SetToolTip(nudList(portNo), str)
                Next
            End If
        Next

    End Sub

End Class